#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "project.h"
#include "language.h"
#include "database.h"

int
main(int argc, char *argv[])
{
	check_language();

	int ret;
	char id[10];
	struct pedigree_list_unit p_l_unit;
	char* args = getenv("QUERY_STRING");

	init_pedigree_list_unit(&p_l_unit);

	ret = getval(id, "id", args);
	
	char* request_url = getenv("REQUEST_URI");
	request_url += CGI_DIR_LENGTH;
	if (ret == 1) {
		ret = get_data(atoi(id), &p_l_unit);
		if (ret == 0) {
			page_header(p_l_unit.self.name, NULL, request_url);
			page_list(p_l_unit);
		} else {
			page_header(G_ERROR, NULL, request_url);
			page_error(ret);
		}
		goto END;
	} else {
		page_header(G_LIST, NULL, request_url);
		page_list_home();
		goto END;
	}

END:
	//printf("%s, %s", args, argv[0]);//debug
	page_footer();
	return 0;
}

int
page_list(struct pedigree_list_unit p_l_unit)
{
	int i;

	fprintf(stdout, "<table border=0 width=90%>\n<tr>\n<td>\n");
	fprintf(stdout, "<table border=0 width=100%>\n");

	fprintf(stdout, "<tr>\n<td width=50%>\n%s: \n\n", G_FATHER);
	if (p_l_unit.father.id != -1 && p_l_unit.father.name != NULL) {
		fprintf(stdout, "<a href=%s&id=%d>%s</a>\n",
			G_LIST_PAGE_NAME, p_l_unit.father.id, p_l_unit.father.name);
	}else {
		fprintf(stdout, "N/A\n");
	}
	fprintf(stdout, "</td>\n<td>\n%s: \n", G_MOTHER);
	if (p_l_unit.mother.id != -1 && p_l_unit.mother.name != NULL) {
		fprintf(stdout, "<a href=%s&id=%d>%s</a><br \\>\n",
			G_LIST_PAGE_NAME, p_l_unit.mother.id, p_l_unit.mother.name);
 	}else {
		fprintf(stdout, "N/A\n");
	}
	fprintf(stdout, "</td>\n<tr>\n<td>\n%s: \n", G_NAME);
	if (p_l_unit.self.name != NULL) {
		fprintf(stdout, "%s", p_l_unit.self.name);
	}

	fprintf(stdout, "</td>\n<td>\n%s: \n\n", G_WIFE);
	for (i = 0; i < p_l_unit.wife_num; i++) {
		if (p_l_unit.wife[i].name != NULL) {
			fprintf(stdout, "<a href=%s&id=%d>%s</a>&nbsp;&nbsp;",
				G_LIST_PAGE_NAME, p_l_unit.wife[i].id, p_l_unit.wife[i].name);
		}else {
			fprintf(stdout, "NO_NAME&nbsp;&nbsp;");
		}
	}
	if (p_l_unit.wife_num <= 0) {
		fprintf(stdout, "N/A\n");
	}

	fprintf(stdout, "</td></tr><tr><td colspan=2>%s: \n", G_CHILDREN);
	for (i = 0; i < p_l_unit.children_num; i++) {

		if (p_l_unit.children[i].name != NULL) {
			fprintf(stdout, "<a href=%s&id=%d>%s</a>&nbsp;&nbsp;",
				G_LIST_PAGE_NAME, p_l_unit.children[i].id, p_l_unit.children[i].name);
		}else {
			fprintf(stdout, "NO_NAME%d&nbsp;&nbsp;", i);
		}
	}
	if (p_l_unit.children_num <= 0) {
		fprintf(stdout, "N/A\n");
	}

	fprintf(stdout, "</td></tr><tr><td colspan=2>%s: ", G_DISCRIPTION);
	if (p_l_unit.dis != NULL) {
		fprintf(stdout, "%s", p_l_unit.dis);
	}else {
		fprintf(stdout, "N/A\n");
	}
	fprintf(stdout, "</td></tr></table>\n");

	fprintf(stdout, "</td>\n<td width=250px style='border-left:1px solid #c9d7f1'>\n");
	fprintf(stdout, "&nbsp;\n");
	fprintf(stdout, "</td>\n</tr>\n</table>\n");

	return 0;
}

int
page_list_home()
{
	fprintf(stdout, "<a href=%s&id=1>root</a><br \\>\n", G_LIST_PAGE_NAME);
	fprintf(stdout, "page_list_home<br \\>\n");
	fprintf(stdout, "page_list_home<br \\>\n");
	fprintf(stdout, "page_list_home<br \\>\n");
	fprintf(stdout, "page_list_home<br \\>\n");
	fprintf(stdout, "page_list_home<br \\>\n");
	fprintf(stdout, "page_list_home<br \\>\n");
	fprintf(stdout, "page_list_home<br \\>\n");
	return 0;
}


